package com.buanalintas.sandals.pos.requestdetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.entities.MasterUnit;
import com.buanalintas.sandals.pos.entities.PosMasterSize;
import com.buanalintas.sandals.pos.entities.PosTransactionRequestDetail;
import com.buanalintas.sandals.pos.masterstock.MasterStock;
import com.buanalintas.sandals.pos.requestdetail.AddSize;
import javax.persistence.EntityManager;

public class RequestDetail extends Page {

    private Field requestDetailId;
    private Field requestDetailRequestId;
    private Field requestDetailItemId;
    private Field requestDetailSizeId;
    private Field requestDetailSize;
    private Field requestDetailQuantity;
    private Field requestDetailDescription;

    public RequestDetail() {
        super();
        setTheClass(PosTransactionRequestDetail.class);
        setPageDetail(MasterStock.class);

        requestDetailId = new Field();
        requestDetailId.setName("requestDetailId");
        requestDetailId.setCaption("id");
        requestDetailId.setListProperties("");
        requestDetailId.setInputProperties("");
        getFields().add(requestDetailId);

        requestDetailRequestId = new Field();
        requestDetailRequestId.setName("requestDetailRequestId");
        requestDetailRequestId.setCaption("request");
        requestDetailRequestId.setListProperties("");
        requestDetailRequestId.setInputProperties("");
        getFields().add(requestDetailRequestId);

        requestDetailItemId = new Field();
        requestDetailItemId.setName("requestDetailItemId");
        requestDetailItemId.setCaption("item");
        requestDetailItemId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        requestDetailItemId.setInputProperties(" size=\"30\"");
        requestDetailItemId.setSelectorCondition("t.itemId IN (SELECT t1.itemStockItemId.itemId FROM PosMasterItemStock t1 WHERE t1.itemStockUnitId.unitId=1 AND t1.itemStockStatusId.statusId=1 AND t1.itemStockValue > 0)");
        requestDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"5%\" nowrap"});
        requestDetailItemId.getSelectorColumns().add(new String[]{"itemModel", " Model", "width=\"45%\""});
        requestDetailItemId.getSelectorColumns().add(new String[]{"itemSizeId", " Size", "width=\"45%\""});
        requestDetailItemId.setSelectorOrder("t.itemCode");
        requestDetailItemId.setRequired(true);
        getFields().add(requestDetailItemId);

        requestDetailQuantity = new Field();
        requestDetailQuantity.setName("requestDetailQuantity");
        requestDetailQuantity.setCaption("quantity");
        requestDetailQuantity.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        requestDetailQuantity.setInputProperties(" size=\"3\"");
        requestDetailQuantity.setRequired(true);
        getFields().add(requestDetailQuantity);

        requestDetailSizeId = new Field();
        requestDetailSizeId.setName("requestDetailSizeId");
        requestDetailSizeId.setCaption("size");
        requestDetailSizeId.setListProperties("");
        requestDetailSizeId.setInputProperties("");
        getFields().add(requestDetailSizeId);

        requestDetailSize = new Field();
        requestDetailSize.setName("requestDetailSize");
        requestDetailSize.setCaption("size");
        requestDetailSize.setListProperties(" align=\"right\" width=\"5%\" nowrap");
//        requestDetailSize.setInputProperties(" size=\"3\"");
        requestDetailSize.setEntityRelation(PosMasterSize.class);
        requestDetailSize.setInputType(Field.INPUT_CHECK_BOX);
        requestDetailSize.setRequired(true);
        getFields().add(requestDetailSize);

        requestDetailDescription = new Field();
        requestDetailDescription.setName("requestDetailDescription");
        requestDetailDescription.setCaption("description");
        requestDetailDescription.setListProperties(" width=\"55%\"");
        requestDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        requestDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        requestDetailDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(requestDetailDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(requestDetailItemId);
        getFieldsAdd().add(requestDetailSize);
        getFieldsAdd().add(requestDetailQuantity);
        getFieldsAdd().add(requestDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(requestDetailItemId);
        getFieldsEdit().add(requestDetailSizeId);
        getFieldsEdit().add(requestDetailQuantity);
        getFieldsEdit().add(requestDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(requestDetailItemId);
        getFieldsList().add(requestDetailSizeId);
        getFieldsList().add(requestDetailQuantity);
        getFieldsList().add(requestDetailDescription);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
    }

    @Override
    public StringBuffer process() {
        if (getAction().equals(Action.TRANS_DETAIL_ADD)) {
            return AddSize.process(this);
        }
//        if (getAction().equals(Action.TRANS_DETAIL_LIST)) {
//            return ListTransaction.process(this);
//        }
        return super.process();
    }

    
    public static MasterUnit getUnit(EntityManager em) {
        try {
            javax.persistence.Query query = em.createQuery("SELECT t FROM MasterUnit t WHERE t.unitId=1");
            return (MasterUnit) query.getSingleResult();
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public void setController(Controller controller) {
        super.setController(controller);
    }
//    @Override
//    public String validate(Object data) {
//        EntityManager em = getEntityManager();
//        PosMasterStatus statusGood = Status.getStatusGood(em);
//        PosTransactionRequestDetail requestDetail = (PosTransactionRequestDetail) data;
//        PosMasterItemStock stock = Item.getStock(em, requestDetail.getRequestDetailItemId(), getUnit(em), statusGood);
//        if (requestDetail.getRequestDetailQuantity().compareTo(stock.getItemStockValue()) > 0 || requestDetail.getRequestDetailQuantity().equals(0)) {
//            return requestDetail.getRequestDetailDescription() + " " + Dictionary.getValue(this, "insufficient inventory stock") + ", " + Dictionary.getValue(this, "current inventory stock: ") + ": " + stock.getItemStockValue() + " " + Dictionary.getValue(this, "will be request") + ": " + requestDetail.getRequestDetailQuantity();
//        }
//        return null;
//    }
}
